package com.project.webservice.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.util.JSONUtils;
import com.project.webservice.dto.GetRidTidLivDto;
import com.project.webservice.dto.SearchGetQualityTidDayDto;
import com.project.webservice.dto.TbRidRunDayDto;
import com.project.webservice.entity.JkXlbcsk;
import com.project.webservice.entity.JkXlzjMid;
import com.project.webservice.entity.TbRidRunDay;
import com.project.webservice.entity.TbRidVender;
import com.project.webservice.mapper.TbRidRunDayMapper;
import com.project.webservice.mapper.TbRidVenderMapper;
import com.project.webservice.service.JkXlbcskService;
import com.project.webservice.service.TbRidRunDayService;
import com.project.webservice.service.TbRidVenderService;
import com.project.webservice.util.SpringBeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class TbRidRunDayServiceImpl extends ServiceImpl<TbRidRunDayMapper, TbRidRunDay> implements TbRidRunDayService {
    @Resource
    private TbRidVenderService tbRidVenderService;
    @Resource
    private JkXlbcskService jkXlbcskService;
    @Override
    @DS("rfidrun")
    public IPage searchByPage(Page page, TbRidRunDayDto search) {
        LambdaQueryWrapper<TbRidRunDay> queryWrapper = convert(search);
        return baseMapper.selectPage(page, queryWrapper);
    }

    private  LambdaQueryWrapper<TbRidRunDay> convert(TbRidRunDayDto search){
        LambdaQueryWrapper<TbRidRunDay> queryWrapper = new LambdaQueryWrapper<>();

        List<String> ridSerialList = getRidSerialList();
        // 查询指定前缀的rid_serial
        if(CollUtil.isNotEmpty(ridSerialList)){
            queryWrapper.apply("SUBSTRING(rid_serial,1,2 )in ({0})",ridSerialList);
        }

        return queryWrapper;
    }



    public List<String> getRidSerialList(){
       List<TbRidVender> list =  tbRidVenderService.list();
       return CollUtil.isNotEmpty(list)?list.stream().map(TbRidVender::getRidVenderRegnum).collect(Collectors.toList()):null;
    }

    private List<JkXlbcsk> getJkXlbcskList(){
        List<JkXlbcsk> list = jkXlbcskService.list();
        return list;
    }


}
